const {baseService} =require('../../baseService')
const model =require('../../../dao/model/model')
const beansEnter =require('../../../beans/beansEnter')
const mongoose = require('mongoose');


/* 
记得在serviceEnter里注册一下
*/

/* 异常类 */
class locationProfessionServiceError extends Error {
    constructor(error,message='locationProfessionService异常'){
        super()
        this.error = error
        this.message=message
    }
}


class locationProfessionService {

    static ins = new locationProfessionService()
    constructor(){
        this.model = model.modellocationProfession
        this.bean_class  =  beansEnter.locationProfession
        this.expClass = locationProfessionServiceError
    }

    static getIns (){
        if(this.ins){
            return this.ins
        }else{
            return new locationProfessionService()
        }
    }
    

    
  async  create(data){
        let bean_obj = new this.bean_class()
 
        let  result 
        for (const key1 in data) {
            for (const key2 in bean_obj) {
                if(key1 == key2){
                    bean_obj[key2] = data[key1]
                }
            }
        }
    
        try {
            result  =  await new this.model(bean_obj).save()
        } catch (error) {
            throw new this.expClass(error)
        }
         if(result){
            return result
         }else{
            throw  new this.expClass()
         }
    }

  async   findByIdAndRemove(data){
        let  result
        try {
            result = await  this.model.findByIdAndRemove(data.id)
        } catch (error) {
            throw new this.expClass(error)
        }

        if(result){
            return result
         }else{
            throw  new this.expClass()
         }
    }

    async   findOneByObjAndDelete(data){
        let  result
        try {
            result = await  this.model.findOneAndDelete(data.id)
        } catch (error) {
            throw new this.expClass(error)
        }

        if(result){
            return result
         }else{
            throw  new this.expClass()
         }
    }

    
    async findByIdAndUpdate(id,updateContent){
        let  result
        try {
            result = await  this.model.findByIdAndUpdate(id,updateContent)
        } catch (error) {
            throw new this.expClass(error)
        }

   

        if(result){
            return result
         }else{
            throw  new this.expClass()
         }
    }

    async queryAll(param1,param2){
        let  result


        if(arguments.length==0 ){
            try {
                result = await  this.model.find()
            } catch (error) {
                throw new this.expClass(error)
            }
            if(result){
                return result
             }else{
                throw  new this.expClass()
             }
        }else if(arguments.length==1){
            try {
                result = await  this.model.find().populate(param1)
            } catch (error) {
                throw new this.expClass(error)
            }
            if(result){
                return result
             }else{
                throw  new this.expClass()
             }
        }else if(arguments.length==2){
            try {
                result = await  this.model.find().populate(param1).populate(param2)
            } catch (error) {
                throw new this.expClass(error)
            }
            if(result){
                return result
             }else{
                throw  new this.expClass()
             }

        }




    }

    async findByIdAndPopulate(id,param1,param2){
        let  result
        if(arguments.length==1){
            try {
                result = await  this.model.findById(id)
            } catch (error) {
                throw new this.expClass(error)
            }
            if(result){
                return result
             }else{
                throw  new this.expClass()
             }
        }else if(arguments.length==2){
            try {
                result = await  this.model.findById(id).populate(param1)
            } catch (error) {
                throw new this.expClass(error)
            }
            if(result){
                return result
             }else{
                throw  new this.expClass()
             }
        }else if(arguments.length==3){
            try {
                result = await  this.model.findById(id).populate(param1).populate(param2)
            } catch (error) {
                throw new this.expClass(error)
            }
            if(result){
                return result
             }else{
                throw  new this.expClass()
             }

        }

    }



    async findOne(obj){
        let  result
        try {
            result = await  this.model.findOne(obj)
        } catch (error) {
            throw new this.expClass(error)
        }

        if(result){
            return result
         }else{
            return null
         }

    }

    // findByObjAndPopulate
    async findByObj(obj,param1,param2){
        if(arguments.length==1){
            let  result
            try {
                result = await  this.model.find(obj)
            } catch (error) {
                throw new this.expClass(error)
            }
    
            if(result){
                return result
             }else{
                throw  new this.expClass()
             }
        }else if(arguments.length==2){
     
            let  result
            try {
                result = await  this.model.find(obj).populate(param1)
            } catch (error) {
                throw new this.expClass(error)
            }
    
            if(result){
                return result
             }else{
                throw  new this.expClass()
             }
        }else if(arguments.length==3){
            let  result
            try {
                result = await  this.model.find(obj).populate(param1).populate(param2)
            } catch (error) {
                throw new this.expClass(error)
            }
    
            if(result){
                return result
             }else{
                throw  new this.expClass()
             }
        }


    }

    async find(obj){
        let  result
        try {
            result = await  this.model.find(obj)
        } catch (error) {
            throw new this.expClass(error)
        }

        if(result){
            return result
         }else{
            throw  new this.expClass()
         }
    }

    async findOneAndPopulate(obj,param1,param2){
        let result
        if(arguments.length==1){
       
            try {
                result = await  this.model.findOne(obj)
            } catch (error) {
                throw new this.expClass(error)
            }
            if(result){
                return result
             }else{
                throw  new this.expClass()
             }
        }else if(arguments.length==2){
            
           
            try {
                result = await  this.model.findOne(obj).populate(param1)
            } catch (error) {
                throw new this.expClass(error)
            }
            if(result){
                return result
             }else{
                throw  new this.expClass()
             }

        }else if(arguments.length==3){
          
            try {
                result = await  this.model.findOne(obj).populate(param1).populate(param2)
               
            } catch (error) {
                throw new this.expClass(error)
            }
            if(result){
                return result
             }else{
                throw  new this.expClass()
             }
        }
    }

    async findAndPopulate(obj,param1,param2){
        let result
        if(arguments.length==1){
       
            try {
                result = await  this.model.find(obj)
            } catch (error) {
                throw new this.expClass(error)
            }
            if(result){
                return result
             }else{
                throw  new this.expClass()
             }
        }else if(arguments.length==2){
            
           
            try {
                result = await  this.model.find(obj).populate(param1)
            } catch (error) {
                throw new this.expClass(error)
            }
            if(result){
                return result
             }else{
                throw  new this.expClass()
             }

        }else if(arguments.length==3){
          
            try {
                result = await  this.model.find(obj).populate(param1).populate(param2)
               
            } catch (error) {
                throw new this.expClass(error)
            }
            if(result){
                return result
             }else{
                throw  new this.expClass()
             }
        }
    }

    async findByObjAndUpdate(obj,content,options){
        let result

        try {
            result = await  this.model.findOneAndUpdate(obj,content,options).populate('default_title_kemu')
        } catch (error) {
        
            throw new this.expClass(error)
        }
        
        if(result){
            return result
         }else{
            throw  new this.expClass()
         }
    }


    // 更新数组
    async findByObjAndUpdateArray(obj,content,options){
        let result
        try {
            result = await  this.model.findOneAndUpdate(obj,{$addToSet:content},options).populate("professionid")
        } catch (error) {
            throw new this.expClass(error)
            // 
        }
        if(result){
            return result
         }else{
            throw  new this.expClass()
         }
    }


    // 存在则更新,不存在则新增
    async findOneAndUpdate(conditions,update){

        let options={"new":true}

        let  result

        try {
            result = await  this.model.findOneAndUpdate(conditions,update,options)
        } catch (error) {
            throw new this.expClass(error)
        }

        if(result){
            return result
         }else{
            throw  new this.expClass()
         }
    }



}







module.exports={locationProfessionService}
